<?php

namespace App\Controller;

use App\Entity\GiftLog;
use App\Entity\GiftLog as Entity;
use App\Entity\User;
use App\Repository\GiftLogRepository;
use App\Repository\UserRepository;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use function Symfony\Component\DependencyInjection\Loader\Configurator\expr;

/**
 * @Route("/user")
 */
class UserController extends AbstractController
{
    /**
     * @Route("/new", name="user_new", methods={"GET","POST"})
     */
    public function new(DocumentManager $dm,GiftLogRepository $giftLogRepository,UserRepository $userRepository)
    {

        $user = $userRepository->find(8);
        dd($user->getName());
        $qb = $dm->createQueryBuilder(GiftLog::class);
        $data = $qb->setQueryArray([
            "user.isBan"=>false
        ])->getQuery()->execute()->toArray();

        dd($data);
//
        $qb = $dm->createQueryBuilder(Entity::class);
        $qb->setQueryArray();
        $qba = $giftLogRepository->createAggregationBuilder();
        $data =  $qba->lookup("user")->alias("user")
            ->match()
            ->execute([])->toArray();

//        dd($data);

//        $data = $giftLogRepository->findBy(array(
//            "user.name"=>['$regex'=>"234"]
//        ));

        dd($data);
        return false;

        $user = new User();
        $user->setName("nihs阿斯顿发斯蒂芬dfasdfdjje")
            ->setAge(60)
            ->setCreateAt(new \DateTime())
            ->setFavor("456a4sdffasdf");
        $dm->persist($user);
        $dm->flush();
//
        $condition = [
            'id' => ['$gt' => 5],
            '$and' => [
                ['favor' => ["\$regex" => "我"]],
                ['name' => ["\$regex" => "你"]]
            ]
        ];
        //普通查询
        $qb = $dm->createQueryBuilder(User::class);
        $qb->setQueryArray($condition);
        $data = $qb->getQuery()->toArray();
        dump($data);
        //计数
        $qb->count();
        $data1 = $qb->getQuery()->execute();
        dump($data1);
        //聚合
        $qb = $dm->createAggregationBuilder(User::class);
        $qb->group()
            ->field("id")
            ->expression("\$age")
            ->field('maxCreateAt')
            ->max('$createAt')
            ->field('count')
            ->sum(1)
        ;
        $data2 = $qb->execute()->toArray();
        dump($data2);

    }
}
